package com.jsjosem.aftersales.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.data.model.Service;
import com.jsjosem.aftersales.utils.ViewUtils;

import java.util.List;

public class ServiceItemsAddAdapter extends BaseAdapter {

    public ServiceItemsAddAdapter(Context context, List<Service> servicesList, ListView listView) {
        this.context = context;
        this.servicesList = servicesList;
        this.listView = listView;
    }

    private ListView listView;
    private Context context;
    private List<Service> servicesList;

    @Override
    public int getCount() {
        return servicesList.size();
    }

    @Override
    public Service getItem(int position) {
        return servicesList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.service_item_add_list, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
//        Service item = getItem(position);

        if (servicesList.get(position).getName() != null) {
            viewHolder.serviceName.setText(servicesList.get(position).getName());
        }
        if (servicesList.get(position).getCount() != null) {
            viewHolder.serviceCount.setText(servicesList.get(position).getCount().toString());
        }
        if (servicesList.get(position).getUnit() != null) {
            viewHolder.serviceUnit.setText(servicesList.get(position).getUnit());
        }
        if (servicesList.get(position).getCost() != null) {
            viewHolder.servicePrice.setText(servicesList.get(position).getCost().toString());
        }

        viewHolder.serviceDel.setOnClickListener(v -> {
            servicesList.remove(position);
            notifyDataSetChanged();
            ViewUtils.setListViewHeightBasedOnChildren(listView);
        });

        //设置EditText的监听
        viewHolder.serviceName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                servicesList.get(position).setName(s.toString());
            }

        });

        viewHolder.serviceCount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                servicesList.get(position).setCount(s.toString());
            }

        });

        viewHolder.serviceUnit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                servicesList.get(position).setUnit(s.toString());
            }

        });

        viewHolder.servicePrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                servicesList.get(position).setCost(s.toString());
            }

        });


        return convertView;
    }

    public static class ViewHolder {
        EditText serviceName;
        EditText serviceCount;
        EditText serviceUnit;
        EditText servicePrice;
        ImageView serviceDel;

        public ViewHolder(View view) {
            this.serviceName = view.findViewById(R.id.service_name);
            this.serviceCount = view.findViewById(R.id.service_count);
            this.serviceUnit = view.findViewById(R.id.service_unit);
            this.servicePrice = view.findViewById(R.id.service_cost);
            this.serviceDel = view.findViewById(R.id.service_del);
        }
    }

}
